package com.aaa.forum.controller;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.config.AppConfig;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Article;
import com.aaa.forum.model.ArticleReply;
import com.aaa.forum.model.User;
import com.aaa.forum.services.IArticleReplyService;
import com.aaa.forum.services.IArticleService;
import com.aaa.forum.services.IReplyLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(tags = "评论点赞接口")
@Slf4j
@RestController
@RequestMapping("/rlike")
public class ReplyLikeController {

    @Resource
    private IReplyLikeService replyLikeService;

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    @ApiOperation("点赞")
    @PostMapping("/like")
    private AppResult like(HttpServletRequest request,
                           @ApiParam("评论id") @RequestParam("replyId") Long replyId) {
        // 校验评论是否存在
        ArticleReply articleReply = articleReplyService.selectById(replyId);
        if (articleReply == null || articleReply.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_REPLY_NOT_EXISTS));
        }
        // 校验帖子是否存在
        Article article = articleService.selectById(articleReply.getArticleId());
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 获取用户id
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用Service
        replyLikeService.like(user.getId(), replyId);
        return AppResult.success();
    }

    @ApiOperation("取消点赞")
    @PostMapping("/cancel")
    private AppResult cancel(HttpServletRequest request,
                             @ApiParam("评论id") @RequestParam("replyId") Long replyId) {
        // 校验评论是否存在
        ArticleReply articleReply = articleReplyService.selectById(replyId);
        if (articleReply == null || articleReply.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_REPLY_NOT_EXISTS));
        }
        // 校验帖子是否存在
        Article article = articleService.selectById(articleReply.getArticleId());
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 获取用户id
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用Service
        replyLikeService.cancel(user.getId(), replyId);
        return AppResult.success();
    }
}
